package it.polimi.swim.servlet;

import it.polimi.swim.entity.SystemUser;
import it.polimi.swim.session.FriendManagerBeanRemote;
import it.polimi.swim.session.SystemUserManagerBeanRemote;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SendFriendRequestServlet
 */
public class SendFriendRequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    public SendFriendRequestServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			System.out.println("Create Friend");
			Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL,"localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			
			String skill;
			
			InitialContext ctx = new InitialContext();
			Object ref = ctx.lookup("SystemUserManagerBean/remote");
			SystemUserManagerBeanRemote systemUserManager = (SystemUserManagerBeanRemote) ref;
			Object ref2 = ctx.lookup("FriendManagerBean/remote");
			FriendManagerBeanRemote friendManager = (FriendManagerBeanRemote) ref2;
			
			HttpSession session = request.getSession();
			
			// cerca sender
			int idSender =(Integer) session.getAttribute("id");
			SystemUser sender = systemUserManager.searchUserById(idSender);
			
			// cerce receiver
			int idReceiver = Integer.parseInt(request.getParameter("idUser"));
			SystemUser receiver = systemUserManager.searchUserById(idReceiver);
			List<SystemUser> friendsS = friendManager.showMyFriendshipRequest(idSender);
			List<SystemUser> friendsR = friendManager.showMyFriendshipRequest(idReceiver);
			if(friendsS.contains(receiver)||friendsR.contains(sender)){
				request.setAttribute("errorMessage", "Friendhip request already exists");
			}
			else
				friendManager.createFriendRequest(sender, receiver);
			request.getRequestDispatcher("/homeUser.jsp").forward(request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			System.out.println("Create Friend");
			Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL,"localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			
			String skill;
			
			InitialContext ctx = new InitialContext();
			Object ref = ctx.lookup("SystemUserManagerBean/remote");
			SystemUserManagerBeanRemote systemUserManager = (SystemUserManagerBeanRemote) ref;
			Object ref2 = ctx.lookup("FriendManagerBean/remote");
			FriendManagerBeanRemote friendManager = (FriendManagerBeanRemote) ref2;
			
			HttpSession session = request.getSession();
			int idReceiver = Integer.parseInt(request.getParameter("idUser"));
			System.out.println(idReceiver);
			
			System.out.println("Create Friend");
			int idSender =(Integer) session.getAttribute("id");
			SystemUser sender = systemUserManager.searchUserById(idSender);
			
			SystemUser receiver = systemUserManager.searchUserById(idReceiver);
			friendManager.createFriendRequest(sender, receiver);
			request.getRequestDispatcher("/homeUser.jsp").forward(request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
